package com.lemon.smartsafety.ui.activity.fire.item.inspection;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.entity.baidu.BaiduMapResultEvent;
import com.lemon.smartsafety.http.HttpBack;
import com.lemon.smartsafety.http.HttpOkBase;
import com.lemon.smartsafety.http.HttpPath;
import com.lemon.smartsafety.result.BaseMode;
import com.lemon.smartsafety.result.fire.SuperviseCompanyResult;
import com.lemon.smartsafety.ui.activity.baidu.LemonBaiduActivity;
import com.lemon.smartsafetynew.ui.activity.base.NewBaseActivity;
import com.lemon.smartsafety.utils.CommonUtil;
import com.lemon.smartsafety.utils.JsonUtils;
import com.lemon.smartsafety.utils.LogUtils;
import com.lemon.smartsafety.utils.app.ToastUtils;

import org.greenrobot.eventbus.Subscribe;

import butterknife.Bind;
import butterknife.OnClick;

public class FirefightingCompanyAddressActivity extends NewBaseActivity {

    @Bind(R.id.main_title)
    TextView mainTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @Bind(R.id.txt_title)
    TextView txtTitle;
    @Bind(R.id.lineAll)
    LinearLayout lineAll;
    @Bind(R.id.txt_location)
    TextView txtLocation;
    @Bind(R.id.txt_address)
    TextView txtAddress;
    @Bind(R.id.btn_submit)
    Button btnSubmit;



    private SuperviseCompanyResult.ResultEntity resultEntity;
    protected String mAddress = null;
    protected String mLngLat = null;

    protected String mLng = null;
    protected String mLat = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void getBundleArgs() {
        if (getIntent().getExtras() != null) {
            resultEntity = (SuperviseCompanyResult.ResultEntity) getIntent().getSerializableExtra(BundleKey.FIRE_COMPANY_BASE_INFO);
        }
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_firefighting_company_address;
    }

    @Override
    protected void initTitleView() {
        setBarTitle("经营地址完善");
    }

    @Override
    protected void initView() {


        txtTitle.setText(resultEntity.getCompanyName());
    }

    @Override
    protected void initHttp() {

    }

    @Override
    protected void initList() {

    }

    @Override
    protected void loadList() {

    }

    @Override
    protected void refreshView() {

    }

    @Override
    protected void callFinish() {
        mActivity.finish();
    }


    @OnClick({R.id.txt_location, R.id.btn_submit})
    public void onButtonClick(View v) {
        switch (v.getId()) {
            case R.id.txt_location:
                startActivity(new Intent(this, LemonBaiduActivity.class));
                break;
            case R.id.btn_submit:
                httpSave();
                break;
            default:
                break;
        }
    }


    @Subscribe
    public void onEventMainThread(BaiduMapResultEvent event) {

        if (event.getSearchMode() != null) {

            LogUtils.LOGE("定位页面返回地理位置成功");

            mAddress = event.getSearchMode().getAddress();
            //替换掉国家
            if (!CommonUtil.strIsEmpty(mAddress)) {
                mAddress = mAddress.replace(event.getSearchMode().getCountry(), "");
            }
            mLngLat = event.getSearchMode().getLatLng().longitude + "," + event.getSearchMode().getLatLng().latitude;
            mLng = String.valueOf(event.getSearchMode().getLatLng().longitude);
            mLat = String.valueOf(event.getSearchMode().getLatLng().latitude);

            txtAddress.setText(mAddress);

        }


    }


    private void httpSave() {
        if (CommonUtil.strIsEmpty(txtAddress.getText().toString().trim())) {
            mHUDUtil.showInfoTip("请定位经营地址");

            return;
        }


        LemonRequestParams params = new LemonRequestParams("application_user_bind", "bind_id", "");
        params.put("company_code", resultEntity.getCompanyCode());
        params.put("longitude", mLng);
        params.put("latitude", mLat);
        params.put("address", txtAddress.getText().toString().trim());

        mHUDUtil.show();
        HttpOkBase.httpBasePost(params, HttpPath.getmBase(HttpPath.Set_company_location), "", new HttpOkBase.HttpInterface() {
            @Override
            public void callBackFunction(boolean httpClient, String result) {

                mHUDUtil.hidden();
                if (httpClient) {
                    BaseMode baseMode = JsonUtils.fromJson(result, BaseMode.class);
                    if (!CommonUtil.objIsEmpty(baseMode)) {
                        if (HttpBack.httpBackError(baseMode)) {
                            ToastUtils.toastMsg(baseMode.getMsg());
                            //mHUDUtil.showDoneTip(baseMode.getMsg());

                            callFinish();
                        } else {
                            mHUDUtil.showErrorTip(baseMode.getMsg());
                        }
                    }
                }
            }
        });


    }


}
